<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/15
 * Time: 16:24
 */

namespace service\k3\util;


use service\k3\Reader;
use think\Db;

abstract class AbstractReader implements Reader
{
    /**
     * 视图名字
     *
     * @var string
     */
    protected $table = '';

    /**
     * sql语句
     *
     * @var string
     */
    protected $sql = '';

    protected $baseSql = '';

    protected $field = '';

    public function __construct()
    {
        $this->setTable();
        $this->setFiled();
        $this->setSql();
        $this->setBaseSql();
    }

    /**
     * 设置表的名字
     * @return mixed
     */
    abstract function setTable();

    /**
     * 设置sql语句
     * @return mixed
     */
    abstract function setSql();

    /**
     * 查询数据
     *
     * @param string $condition
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function read($condition = '')
    {
        if(empty($condition)){
            $sql = $this->baseSql;
        }elseif(is_string($condition)){
            $sql = sprintf($this->sql, $condition);
        }elseif(is_array($condition)){
            $condition = '(\''.implode("','", $condition).'\')';
            $this->sql = str_replace('=',' in ',$this->sql);
            $sql = sprintf($this->sql,$condition);
            $sql =str_replace('\'(','(',$sql);
            $sql =str_replace(')\'',')',$sql);
        }
//        return Db::connect(self::K3_DB_CONNECT_CONFIG)->query($sql);
        return Db::connect('sqlsrv://sql_erp:ERP0.123#@10.11.8.2:1433/AIS2018PRTTECH#utf8')->query($sql);
    }
}
